package com.dt.dubai.core.qrcode.barCodeUtil;

import org.apache.commons.lang3.StringUtils;
import org.krysalis.barcode4j.impl.codabar.CodabarBean;
import org.krysalis.barcode4j.impl.code128.Code128Bean;
import org.krysalis.barcode4j.impl.int2of5.Interleaved2Of5Bean;
import org.krysalis.barcode4j.impl.pdf417.PDF417Bean;
import org.krysalis.barcode4j.impl.upcean.EAN13Bean;
import org.krysalis.barcode4j.impl.upcean.UPCABean;
import org.krysalis.barcode4j.impl.upcean.UPCEBean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.krysalis.barcode4j.tools.UnitConv;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 一维条形码类
 * @data 2019-05-07
 * @author pillar
 */
public class barCodeUtil {
    /**
     *具体实现 Code 128
     * @param msg
     * @param path
     */
    public static void getBarCode128(String msg,String path){
        try {
            File file=new File(path);
            OutputStream ous=new FileOutputStream(file);
            if(StringUtils.isEmpty(msg) || ous==null)
                return;
            //选择条形码类型(好多类型可供选择)
            Code128Bean bean=new Code128Bean();
            //设置长宽
            final int resolution=200;
            final double moduleWidth = UnitConv.in2mm(1.0f / resolution);
            bean.setModuleWidth(moduleWidth);
            bean.doQuietZone(false);
            String format = "image/png";
            // 输出流
            BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, format,
                    resolution, BufferedImage.TYPE_BYTE_BINARY, false, 0);
            //生成条码
            bean.generateBarcode(canvas,msg);
            canvas.finish();
        }catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     *具体实现 EAN-13
     * @param msg
     * @param path
     */
    public static void getBarEAN13(String msg,String path){
        try {
            File file=new File(path);
            OutputStream ous=new FileOutputStream(file);
            if(StringUtils.isEmpty(msg) || ous==null)
                return;
            //选择条形码类型(好多类型可供选择)
            EAN13Bean bean=new EAN13Bean();
            //设置长宽
            final int resolution=150;
            final double moduleWidth = UnitConv.in2mm(1.0f / resolution);
            bean.setModuleWidth(moduleWidth);
            bean.doQuietZone(false);
            String format = "image/png";
            // 输出流
            BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, format,
                    resolution, BufferedImage.TYPE_BYTE_BINARY, false, 0);
            //生成条码
            bean.generateBarcode(canvas,msg);
            canvas.finish();
        }catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     *具体实现 PDF417
     * @param msg
     * @param path
     */
    public static void getBarPDF417(String msg,String path){
        try {
            File file=new File(path);
            OutputStream ous=new FileOutputStream(file);
            if(StringUtils.isEmpty(msg) || ous==null)
                return;
            //选择条形码类型(好多类型可供选择)
            PDF417Bean bean=new PDF417Bean();
            //设置长宽
            final int resolution=150;
            final double moduleWidth = UnitConv.in2mm(1.0f / resolution);
            bean.setModuleWidth(moduleWidth);
            bean.doQuietZone(false);
            String format = "image/png";
            // 输出流
            BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, format,
                    resolution, BufferedImage.TYPE_BYTE_BINARY, false, 0);
            //生成条码
            bean.generateBarcode(canvas,msg);
            canvas.finish();
        }catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     *具体实现 Codabar
     * @param msg
     * @param path
     */
    public static void getBarCodabar(String msg,String path){
        try {
            File file=new File(path);
            OutputStream ous=new FileOutputStream(file);
            if(StringUtils.isEmpty(msg) || ous==null)
                return;
            //选择条形码类型(好多类型可供选择)

            CodabarBean bean=new CodabarBean();
            //设置长宽
            final int resolution=150;
            final double moduleWidth = UnitConv.in2mm(1.0f / resolution);
            bean.setModuleWidth(moduleWidth);
            bean.doQuietZone(false);
            String format = "image/png";
            // 输出流
            BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, format,
                    resolution, BufferedImage.TYPE_BYTE_BINARY, false, 0);
            //生成条码
            bean.generateBarcode(canvas,msg);
            canvas.finish();
        }catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     *具体实现 Interleaved 2 of 5
     * @param msg
     * @param path
     */
    public static void getBarInterleaved2Of5(String msg,String path){
        try {
            File file=new File(path);
            OutputStream ous=new FileOutputStream(file);
            if(StringUtils.isEmpty(msg) || ous==null)
                return;
            //选择条形码类型(好多类型可供选择)

            Interleaved2Of5Bean bean=new Interleaved2Of5Bean();
            //设置长宽
            final int resolution=150;
            final double moduleWidth = UnitConv.in2mm(1.0f / resolution);
            bean.setModuleWidth(moduleWidth);
            bean.doQuietZone(false);
            String format = "image/png";
            // 输出流
            BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, format,
                    resolution, BufferedImage.TYPE_BYTE_BINARY, false, 0);
            //生成条码
            bean.generateBarcode(canvas,msg);
            canvas.finish();
        }catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     *具体实现 UPCA
     * @param msg
     * @param path
     */
    public static void getBarUPCA(String msg,String path){
        try {
            File file=new File(path);
            OutputStream ous=new FileOutputStream(file);
            if(StringUtils.isEmpty(msg) || ous==null)
                return;
            //选择条形码类型(好多类型可供选择)

            UPCABean bean=new UPCABean();
            //设置长宽
            final int resolution=150;
            final double moduleWidth = UnitConv.in2mm(1.0f / resolution);
            bean.setModuleWidth(moduleWidth);
            bean.doQuietZone(false);
            String format = "image/png";
            // 输出流
            BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, format,
                    resolution, BufferedImage.TYPE_BYTE_BINARY, false, 0);
            //生成条码
            bean.generateBarcode(canvas,msg);
            canvas.finish();
        }catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     *具体实现 UPCE
     * @param msg
     * @param path
     */
    public static void getBarUPCE(String msg,String path){
        try {
            File file=new File(path);
            OutputStream ous=new FileOutputStream(file);
            if(StringUtils.isEmpty(msg) || ous==null)
                return;
            //选择条形码类型(好多类型可供选择)
            UPCEBean bean=new UPCEBean();
            //设置长宽
            final int resolution=150;
            final double moduleWidth=0.20;
            bean.setModuleWidth(moduleWidth);
            bean.doQuietZone(false);
            String format = "image/png";
            // 输出流
            BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, format,
                    resolution, BufferedImage.TYPE_BYTE_BINARY, false, 0);
            //生成条码
            bean.generateBarcode(canvas,msg);
            canvas.finish();
        }catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    public static void main(String[] args) {
        String msg = "02144212";
        String msg1 = "111111111111";
        String msg2 = "11111111111";
        String path1 = "C:\\QrCode\\11.png";
        String path2 = "C:\\QrCode\\12.png";
        String path3 = "C:\\QrCode\\13.png";
        String path4 = "C:\\QrCode\\14.png";
        String path5 = "C:\\QrCode\\15.png";
        String path6 = "C:\\QrCode\\16.png";
        String path7 = "C:\\QrCode\\17.png";
        barCodeUtil.getBarUPCE(msg,path1);
        barCodeUtil.getBarCodabar(msg,path2);
        barCodeUtil.getBarCode128(msg,path3);
        barCodeUtil.getBarEAN13(msg1,path4);
        barCodeUtil.getBarInterleaved2Of5(msg,path5);
        barCodeUtil.getBarPDF417(msg1,path6);
        barCodeUtil.getBarUPCA(msg2,path7);

    }
}

